#!/bin/bash
# installation redis

# 官网 redis.io

. ./public_functions &>/dev/null #公共函数库
. /tmp/public_functions &>/dev/null #公共函数库

##1 源码安装redis
redis_Install_centos7() {

  if [[ -f /apps/redis ]] || [[ $(find /run | grep redis) ]] || [[ ! $(rpm -q redis | grep not) ]]; then
    logTips "redis may already exist" "(redis可能已安装,请手动检查)" #1-20#
    exit
  fi

  readonly redis_Version="redis-6.2.6.tar.gz"
  readonly redis_Catalog="redis-6.2.6"
  readonly redis_Install_dir="/apps/redis"
  readonly password="123456"

  logW "1. 开始安装redis所需依赖" #1-20#
  logY "yum install epel-release gcc make jemalloc-devel systemd-devel tar net-tools -y" #1-20#
    yum clean all
    for i in epel-release gcc make jemalloc-devel systemd-devel tar net-tools; do
      yum install $i -y
      checkTF #21#
    done

  logW "2. 开始下载 $redis_Version" #1-20#
  logY "curl -# --output /tmp/$redis_Version https://nginx.org/download/$redis_Version" #1-20#
    if [[ ! -f /tmp/$redis_Version ]]; then
      curl -# --output /tmp/$redis_Version http://download.redis.io/releases/$redis_Version
      checkTF #21#
    else
      logTips "" "$redis_Version Already exists ($redis_Version 安装包已存在)"
    fi

  logW "3. 开始解压 $redis_Version" #1-20#
  logY "tar -xzf /tmp/$redis_Version -C /tmp" #1-20#
    tar -xzf /tmp/$redis_Version -C /tmp
  checkTF #21#

  logW "4. 进入redis安装包目录 /tmp/$redis_Catalog" #1-20#
  logY "cd /tmp/$redis_Catalog" #1-20#
    cd /tmp/$redis_Catalog
  checkTF #21#

  logW "5. 开始编译并安装 redis" #1-20#
  logY "make USE_SYSTEMD=yes PREFIX=${redis_Install_dir} install" #1-20#
    make USE_SYSTEMD=yes PREFIX=${redis_Install_dir} install
  checkTF #21#

  logW "6. 创建软链接" #1-20#
  logY "ln -s ${redis_Install_dir}/bin/redis-* /usr/local/bin/" #1-20#
    ln -s ${redis_Install_dir}/bin/redis-* /usr/local/bin/
  echo

  logW "7. 创建${redis_Install_dir}/redis下相关目录etc log data run" #1-20#
  logY "mkdir -p ${redis_Install_dir}/{etc,log,data,run}" #1-20#
    mkdir -p ${redis_Install_dir}/{etc,log,data,run}
  checkTF #21#
  
  logW "8. 拷贝/tmp/$redis_Catalog/redis.conf文件到${redis_Install_dir}/etc 下" #1-20#
  logY "cp /tmp/$redis_Catalog/redis.conf ${redis_Install_dir}/etc/" #1-20#
    cp /tmp/$redis_Catalog/redis.conf ${redis_Install_dir}/etc/ 
  checkTF #21#

sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' ${redis_Install_dir}/etc/redis.conf
sed -i "/# requirepass/a requirepass $password" ${redis_Install_dir}/etc/redis.conf
sed -i "/^dir .*/c dir ${redis_Install_dir}/data/" ${redis_Install_dir}/etc/redis.conf
sed -i "/logfile .*/c logfile ${redis_Install_dir}/log/redis-6379.log" ${redis_Install_dir}/etc/redis.conf
sed -i "/^pidfile .*/c  pidfile ${redis_Install_dir}/run/redis_6379.pid" ${redis_Install_dir}/etc/redis.conf

  logW "9. 添加用户 redis" #1-20#
  logY "useradd -M -s /sbin/nologin redis" #1-20#
    useradd -M -s /sbin/nologin redis
    
    if [[ $? -eq 0 || $? -eq 9 ]]; then
      logG "success ↑↑↑" #1-20#
      echo " "
    else
      logR "fail ↑↑↑       -->> return $?"
      exit
    fi

  chown -R redis.redis ${redis_Install_dir}

cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF

  sysctl -p >/dev/null

  echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
  chmod +x /etc/rc.d/rc.local
  /etc/rc.d/rc.local
#  echo -e '#!/bin/bash\necho never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
#  chmod +x /etc/rc.local
#  /etc/rc.local

  logW "10. 创建用于redis启动的systemd文件" #1-20#
  logY "cat >/usr/lib/systemd/system/redis.service <<-EOF..." #1-20#
cat >/usr/lib/systemd/system/redis.service <<-EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${redis_Install_dir}/bin/redis-server ${redis_Install_dir}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target

EOF
  checkTF #21#

  logW "11. 启动redis服务" #1-20#
  logY "systemctl enable --now redis" #1-20#
    systemctl daemon-reload
    systemctl enable --now redis 
  checkTF #21#

  logW "10. 放行6379端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="6379"/"tcp" --permanent" #1-20#
      firewall-cmd --zone=public --add-port="6379"/"tcp" --permanent 2>/dev/null
      firewall-cmd --reload &>/dev/null
      echo ""
    else
      logR "防火墙未开启" #1-20#
      echo ""
    fi

    redis-cli -a $password INFO Server 2> /dev/null

}

##2 源码安装redis
redis_Install_ubuntu() {

  if [[ -f /apps/redis ]] || [[ $(sudo find /run | grep redis) ]] ; then
    logTips "redis may already exist" "(redis可能已安装,请手动检查)" #1-20#
    exit
  fi

  readonly redis_Version="redis-6.2.6.tar.gz"
  readonly redis_Catalog="redis-6.2.6"
  readonly redis_Install_dir="/apps/redis"
  readonly password="123456"

  logW "1. 开始安装redis所需依赖" #1-20#
  logY "yum install gcc make jemalloc-devel systemd-devel tar net-tools -y" #1-20#
    sudo apt update
    for i in gcc make libjemalloc-dev libsystemd-dev tar net-tools; do
      sudo apt-get install $i -y
      checkTF #21#
    done

  logW "2. 开始下载 $redis_Version" #1-20#
  logY "curl -# --output /tmp/$redis_Version https://nginx.org/download/$redis_Version" #1-20#
    if [[ ! -f /tmp/$redis_Version ]]; then
      sudo curl -# --output /tmp/$redis_Version http://download.redis.io/releases/$redis_Version
      checkTF #21#
    else
      logTips "" "$redis_Version Already exists ($redis_Version 安装包已存在)"
    fi

  logW "3. 开始解压 $redis_Version" #1-20#
  logY "tar -xzf /tmp/$redis_Version -C /tmp" #1-20#
    sudo tar -xzf /tmp/$redis_Version -C /tmp
  checkTF #21#

  logW "4. 进入redis安装包目录 /tmp/$redis_Catalog" #1-20#
  logY "cd /tmp/$redis_Catalog" #1-20#
    cd /tmp/$redis_Catalog
  checkTF #21#

  logW "5. 开始编译并安装 redis" #1-20#
  logY "make USE_SYSTEMD=yes PREFIX=${redis_Install_dir} install" #1-20#
    sudo make USE_SYSTEMD=yes PREFIX=${redis_Install_dir} install
  checkTF #21#

  logW "6. 创建软链接" #1-20#
  logY "ln -s ${redis_Install_dir}/bin/redis-* /usr/local/bin/" #1-20#
    sudo ln -s ${redis_Install_dir}/bin/redis-* /usr/local/bin/
  echo

  logW "7. 创建${redis_Install_dir}/redis下相关目录etc log data run" #1-20#
  logY "mkdir -p ${redis_Install_dir}/{etc,log,data,run}" #1-20#
    sudo mkdir -p ${redis_Install_dir}/{etc,log,data,run}
  checkTF #21#
  
  logW "8. 拷贝/tmp/$redis_Catalog/redis.conf文件到${redis_Install_dir}/etc 下" #1-20#
  logY "cp /tmp/$redis_Catalog/redis.conf ${redis_Install_dir}/etc/" #1-20#
    sudo cp /tmp/$redis_Catalog/redis.conf ${redis_Install_dir}/etc/ 
  checkTF #21#

sudo sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' ${redis_Install_dir}/etc/redis.conf
sudo sed -i "/# requirepass/a requirepass $password" ${redis_Install_dir}/etc/redis.conf
sudo sed -i "/^dir .*/c dir ${redis_Install_dir}/data/" ${redis_Install_dir}/etc/redis.conf
sudo sed -i "/logfile .*/c logfile ${redis_Install_dir}/log/redis-6379.log" ${redis_Install_dir}/etc/redis.conf
sudo sed -i "/^pidfile .*/c  pidfile ${redis_Install_dir}/run/redis_6379.pid" ${redis_Install_dir}/etc/redis.conf

  logW "9. 添加用户 redis" #1-20#
  logY "useradd -M -s /sbin/nologin redis" #1-20#
    sudo useradd -M -s /sbin/nologin redis
    
    if [[ $? -eq 0 || $? -eq 9 ]]; then
      logG "success ↑↑↑" #1-20#
      echo " "
    else
      logR "fail ↑↑↑       -->> return $?"
      exit
    fi

  sudo chown -R redis:redis ${redis_Install_dir}

#sudo cat >> /etc/sysctl.conf <<-EOF
#net.core.somaxconn = 1024
#vm.overcommit_memory = 1
#EOF
#
#  sudo sysctl -p >/dev/null

#  sudo echo -e '#!/bin/bash\necho never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
#  sudo chmod +x /etc/rc.local
#  sudo /etc/rc.local

#  logW "10. 创建用于redis启动的systemd文件" #1-20#
#  logY "cat >/usr/lib/systemd/system/redis.service <<-EOF..." #1-20#
#sudo cat >/usr/lib/systemd/system/redis.service <<-EOF
#[Unit]
#Description=Redis persistent key-value database
#After=network.target
#
#[Service]
#ExecStart=${redis_Install_dir}/bin/redis-server ${redis_Install_dir}/etc/redis.conf --supervised systemd
#ExecStop=/bin/kill -s QUIT \$MAINPID
#Type=notify
#User=redis
#Group=redis
#RuntimeDirectory=redis
#RuntimeDirectoryMode=0755
#LimitNOFILE=1000000
#
#[Install]
#WantedBy=multi-user.target
#
#EOF
#  checkTF #21#

  logW "10. 启动redis服务" #1-20#
  logY "/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd &" #1-20#
    sudo /apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd &
  checkTF #21#

  logW "11. 放行6379端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="6379"/"tcp" --permanent" #1-20#
      sudo firewall-cmd --zone=public --add-port="6379"/"tcp" --permanent 2>/dev/null
      sudo firewall-cmd --reload &>/dev/null
      echo ""
    else
      logR "防火墙未开启" #1-20#
      echo ""
    fi

    sudo redis-cli -a $password INFO Server 2> /dev/null

}

check_Version #25#

systemtype=$(uname -a | grep -Eo '(ubuntu|centos7)')

if [[ $systemtype == centos7 ]]; then 
   redis_Install_centos7  ##1

elif [[ $systemtype == ubuntu ]]; then
     redis_Install_ubuntu  ##2

fi
